package android.concisapp.parser;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class ParserListNews {

	ArrayList<RssItem> arrayItem;
	RssItem item;

	public ParserListNews() {
		try {
			URL url = new URL(
					"http://madridhaceruido.blogspot.com/feeds/posts/default?alt=rss");
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

			factory.setNamespaceAware(false);
			XmlPullParser xpp = factory.newPullParser();
			xpp.setInput(url.openConnection().getInputStream(), "UTF_8");
			// xpp.setInput(getInputStream(url), "UTF-8");

			boolean insideItem = false;

			// Returns the type of current event: START_TAG, END_TAG, etc..
			int eventType = xpp.getEventType();
			arrayItem = new ArrayList<RssItem>();
			item = new RssItem();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					if (xpp.getName().equalsIgnoreCase("item")) {
//						Log.d("FEED ITEM!", "PASA");
						insideItem = true;
					} else if (xpp.getName().equalsIgnoreCase("guid")) {
						if (insideItem) {
							item.setGuid(xpp.nextText());
//							Log.i("FEED GUID!", item.getGuid());
						}
					} else if (xpp.getName().equalsIgnoreCase("pubDate")) {
						if (insideItem) {
							item.setPubDate(xpp.nextText());
//							Log.i("FEED DATE!", item.getPubDate());
						}
					} else if (xpp.getName().equalsIgnoreCase("title")) {
						if (insideItem) {
							item.setTitle(xpp.nextText());
//							Log.i("FEED TITLE!", item.getTitle());
						}
					} else if (xpp.getName().equalsIgnoreCase("description")) {
						if (insideItem) {
							item.setDescription(xpp.nextText());
//							Log.i("FEED DESCRIPTION!", item.getDescription());
						}
					} else if (xpp.getName().equalsIgnoreCase("link")) {
						if (insideItem) {
							item.setLink(xpp.nextText());
//							Log.i("FEED LINK!", item.getLink());
						}
					}else if (xpp.getName().equalsIgnoreCase("media:thumbnail")) {
						if (insideItem) {
							item.setThumbnail(xpp.getAttributeValue(1));
//							Log.i("FEED LINK PHOTO!", item.getThumbnail());
						}
					}
				} else if (eventType == XmlPullParser.END_TAG
						&& xpp.getName().equalsIgnoreCase("item")) {
					insideItem = false;
					arrayItem.add(item);
					item = new RssItem();
				}
				eventType = xpp.next(); // move to next element
			}
//			Log.i("FEED RESULT!", "tama�o " + arrayItem.size());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();

		}
	}

	public ArrayList<RssItem> getArrayItem() {
		return arrayItem;
	}

	public void setArrayItem(ArrayList<RssItem> arrayItem) {
		this.arrayItem = arrayItem;
	}

}
